package com.bdmc.web.controller.work;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Arrays;

import com.bdmc.base.domain.BasePerson;
import com.bdmc.base.service.IBasePersonService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.work.domain.vo.WorkOrderVo;
import com.bdmc.work.domain.bo.WorkOrderBo;
import com.bdmc.work.service.IWorkOrderService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 清运任务工单控制器
 *
 * @author bdmc
 * @date 2022-03-30
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/work/order")
public class WorkOrderController extends BaseController {

    private final IWorkOrderService iWorkOrderService;

    private final IBasePersonService personService;
    /**
     * 查询清运任务工单列表
     */
    @SaCheckPermission("work:order:list")
    @GetMapping("/list")
    public TableDataInfo<WorkOrderVo> list(@Validated(QueryGroup.class) WorkOrderBo bo, PageQuery pageQuery) {
        return iWorkOrderService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出清运任务工单列表
     */
    @SaCheckPermission("work:order:export")
    @Log(title = "清运任务工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated WorkOrderBo bo, HttpServletResponse response) {
        List<WorkOrderVo> list = iWorkOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "清运任务工单", WorkOrderVo.class, response);
    }

    /**
     * 获取清运任务工单详细信息
     */
    @SaCheckPermission("work:order:query")
    @GetMapping("/{orderId}")
    public AjaxResult<WorkOrderVo> getInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(iWorkOrderService.queryById(orderId));
    }

    /**
     * 新增清运任务工单
     */
    @SaCheckPermission("work:order:add")
    @Log(title = "清运任务工单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody WorkOrderBo bo) {
        BasePerson clearer = personService.getById(bo.getCleanerId());
        bo.setDeptId(clearer.getDeptId());
        bo.setOrderStatus(1);
        bo.setOrderTime(LocalDateTime.now());
        return toAjax(iWorkOrderService.insertByBo(bo));
    }

    /**
     * 修改清运任务工单
     */
    @SaCheckPermission("work:order:edit")
    @Log(title = "清运任务工单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody WorkOrderBo bo) {
        return toAjax(iWorkOrderService.updateByBo(bo));
    }

    /**
     * 删除清运任务工单
     */
    @SaCheckPermission("work:order:remove")
    @Log(title = "清运任务工单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] orderIds) {
        return toAjax(iWorkOrderService.deleteWithValidByIds(Arrays.asList(orderIds), true));
    }
}
